const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '新食堂食材进货清单',
    totalPrice: 0,
    list: [],
    inputValue: "",
    activeName: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    activeName2: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    readonly: false,
  },

  bindKeyInput: function (e) {
    let { value } = e.detail
    value = parseInt(value)
    const { list, type } = this.data
    if (type === 0) {
      const { stockOrdermxId } = e.currentTarget.dataset.record
      list.map(item => {
        if (item.stockOrdermxId === stockOrdermxId) {
          item.unitPrice = value
        }
      })

    } else if (type === 1) {
      const { auxiliaryMaterialsmxId } = e.currentTarget.dataset.record
      list.map(item => {
        if (item.auxiliaryMaterialsmxId === auxiliaryMaterialsmxId) {
          item.unitPrice = value
        }
      })
    } else {
      const { smallCanteenPurchasemxId } = e.currentTarget.dataset.record
      list.map(item => {
        if (item.smallCanteenPurchasemxId === smallCanteenPurchasemxId) {
          item.unitPrice = value
        }
      })
    }
    this.countPrice(list)
    console.log(value)
    this.setData({ list })
  },
  Price: function (e) {
    const { type } = this.data
    const { stockOrdermxId, auxiliaryMaterialsmxId, smallCanteenPurchasemxId } = e.currentTarget.dataset.record
    if (type === 0) {
      app.reqFetch.getPrice({
        unitPrice: e.detail.value,
        status: "1",
        stockOrdermxId,
      }, (res) => {
      })
    } else if (type === 1) {
      app.reqFetch.updateUnit({
        unitPrice: e.detail.value,
        status: "1",
        auxiliaryMaterialsmxId,
      }, (res) => {
      })
    } else {

      app.reqFetch.smallcanteenupdateUnit({
        unitPrice: e.detail.value,
        status: "1",
        smallCanteenPurchasemxId,
      }, (res) => {
      })
    }






  },
  onChange(event) {
    this.setData({
      activeName: event.detail
    });
  },
  changeCollapse2(event) {
    this.setData({
      activeNames2: event.detail
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log()
    const { userName } = app.globalData
    const record = JSON.parse(options.data)
    const { stockOrderId = 0, auxiliaryMaterialsId = 0, smallCanteenPurchaseId = 0, mealTime, readonly = false, type = 0 } = record

    this.setData({
      stockOrderId,
      auxiliaryMaterialsId,
      smallCanteenPurchaseId,
      type,
      readonly,
      mealTime,
      userName
    }, () => {
      this.getOrderDetails()
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getOrderDetails() {

    const { stockOrderId, auxiliaryMaterialsId, smallCanteenPurchaseId, type } = this.data
    const { userId } = app.globalData
    if (type === 0) {
      wx.setNavigationBarTitle({
        title: '新食堂食材进货清单',
      })

      app.reqFetch.selecSupplierOrder({
        supplierId: userId,
        stockOrderId,
      }, (res) => {
        this.countPrice(res)
        this.setData({
          list: res
        })
      })
    } else if (type === 1) {
      wx.setNavigationBarTitle({
        title: '新食堂主食进货清单',
      })
      app.reqFetch.selecSupplierOrder1({
        supplierId: userId,
        orderId: auxiliaryMaterialsId,
      }, (res) => {
        this.countPrice(res)
        this.setData({
          list: res
        })
      })

    } else if (type === 2) {
      wx.setNavigationBarTitle({
        title: '小食堂申请清单',
      })
      app.reqFetch.smallcanteenpurchasemxOrder1({
        supplierId: userId,
        orderId: smallCanteenPurchaseId,
      }, (res) => {
        this.setData({ canteensList: JSON.parse(res[0].boxId) })
        this.countPrice(res)
        this.setData({
          list: res
        })
      })
    }
  },

  countPrice(res = []) {
    let totalPrice = 0
    res.map(item => {
      totalPrice += item.unitPrice * item.purchaseNum
    })
    this.setData({
      totalPrice
    })
  },
  Sbmission: function (e) {
    const { stockOrderId, auxiliaryMaterialsId, smallCanteenPurchaseId, type } = this.data
    if (type === 0) {
      app.reqFetch.updateSupplierStutas({ stockOrderId }, (res) => {
        wx.showToast({
          title: '食材清单报价完成',
          icon: 'none'
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 700)
      })
    } else if (type === 1) {
      app.reqFetch.auxiliarymateriUpdateSupplierOrder({ auxiliaryMaterialsId}, (res) => {
        wx.showToast({
          title: '主食清单报价完成',
          icon: 'none'
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 700)
      })
    } else {
      app.reqFetch.smallcanteenpurchaseUpdateSupplierOrder({ smallCanteenPurchaseId}, (res) => {
        wx.showToast({
          title: '小食堂清单报价完成',
          icon: 'none'
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 700)
      })
    }
  },
  handleCancel() {
    wx.navigateBack({
      delta: 1,
    })
  }
})